package com.rzhkj.core.tools;

import okhttp3.*;
import org.apache.commons.collections.map.HashedMap;

import java.io.IOException;
import java.util.Map;

/**
 * Created by chendehui on 18/2/11.
 */
public class HttpTools {

    public static Map<String, String> notify(String notifictionUrl, Map<String, Object> param) {
        String postBody = JSON.toJSONString(param);
        Request request = new Request.Builder()
                .url(notifictionUrl)
                .addHeader("accept", "application/json")
                .addHeader("content-type", "application/json")
                .post(RequestBody.create(MediaType.parse("application/json;charset=utf-8"), postBody))
                .build();
        return http(request);
    }

    public static Map<String, String> http(Request request) {
        OkHttpClient client = new OkHttpClient.Builder()
                .addInterceptor(new OkhttpLogInterceptor())
                .build();
        Response response = null;
        try {
            response = client.newCall(request).execute();
            String jsonResult = response.body().string();
            Map<String, String> result = new HashedMap();
            result.put("code", String.valueOf(response.code()));
            result.put("result", jsonResult);
            return result;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
